package com.groupseven.happycampus.controller;

import com.groupseven.happycampus.common.pojo.RepairPersonnel;
import com.groupseven.happycampus.common.vo.LayuiPageVo;
import com.groupseven.happycampus.common.pojo.RepairWorkOrder;
import com.groupseven.happycampus.common.result.R;
import com.groupseven.happycampus.common.utils.OssUtils;
import com.groupseven.happycampus.common.vo.TypeWorkBarVo;
import com.groupseven.happycampus.common.vo.WorkOrderPieVo;
import com.groupseven.happycampus.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/repair")
public class RepairController {
    @Autowired
    private RepairService repairService;

    /**
     *职工列表+分页和模糊查询
     */
    @RequestMapping("/personnelList")
    public LayuiPageVo<RepairPersonnel> repairPersonnelPageDate(@RequestParam Map<String, Object> params) {
        return repairService.repairPersonnelPageDate(params);
    }

    /**
     *添加新职工
     */
    @RequestMapping("/addPersonnel")
    public R addPersonnel(RepairPersonnel personnel) {
        System.out.println(personnel);
        int result = repairService.addPersonnel(personnel);
        if (result==1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 实现职工信息修改功能
     * @param personnel 传入修改页面提交的数据
     * @return 返回执行操作情况
     */
    @RequestMapping("/updatePersonnel")
    public R updatePersonnel(RepairPersonnel personnel) {
        System.out.println(personnel);
        int result = repairService.updatePersonnel(personnel);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 实现图片上传功能
     * @param file 文件
     * @return 返回执行操作情况
     */
    @RequestMapping("/upload")
    public R upload(MultipartFile file) throws IOException {
        if (file!=null) {
            String originalFilename = file.getOriginalFilename();
            InputStream inputStream = file.getInputStream();
            String url = OssUtils.uploadFile(inputStream, originalFilename);
            //将 url 发给前端
            return R.ok().data("url", url);
        }
        return R.error();
    }

    /**
     *
     * @param id 传入要删除的 id
     * @return 返回执行操作情况
     */
    @RequestMapping("/deletePersonnel")
    public R deletePersonnel(Integer id) {
        System.out.println(id);
        int result = repairService.deletePersonnel(id);
        if (result==1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 批量删除
     * @param ids 需要删除的 id
     * @return 返回执行操作情况
     */
    @GetMapping("/deletesPersonnel")
    public R deletesPersonnel(String ids) {
        System.out.println(ids);
        int result = repairService.deletesPersonnel(ids);
        if (result>0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 维修模块工种树状图
     * @return 树状图需要的数据
     */
    @RequestMapping("/personnelBar")
    public R echartBar() {
        List<TypeWorkBarVo> list = repairService.typeWorkBarData();
        return R.ok().data("typeWorkBar", list);
    }

//------------------------------- 工单功能 -------------------------------------

    /**
     * 工单列表
     */
    @RequestMapping("/workOrderList")
    public LayuiPageVo<RepairWorkOrder> workOrderPageDate(@RequestParam Map<String, Object> params) {
        return repairService.workOrderPageDate(params);
    }

    /**
     *报单功能
     */
    @RequestMapping("/addWorkOrder")
    public R addWorkOrder(RepairWorkOrder workOrder) {
        System.out.println(workOrder);
        int result = repairService.addWorkOrder(workOrder);
        if (result==1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 实现工单信息修改功能
     * @param workOrder 传入修改页面提交的数据
     * @return 返回执行操作情况
     */
    @RequestMapping("/updateWorkOrder")
    public R updateWorkOrder(RepairWorkOrder workOrder) {
        System.out.println(workOrder);
        int result = repairService.updateWorkOrder(workOrder);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     *完工功能对完工时间赋值，改变列表状态
     */
    @RequestMapping("/giveEndTime")
    public R giveEndTime(RepairWorkOrder workOrder) {
        System.out.println(workOrder);
        int result = repairService.giveEndTime(workOrder);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     *
     * @param id 传入要删除的 id
     * @return 返回执行操作情况
     */
    @RequestMapping("/deleteWorkOrder")
    public R deleteWorkOrder(Integer id) {
        System.out.println(id);
        int result = repairService.deleteWorkOrder(id);
        if (result==1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 批量删除
     * @param ids 需要删除的 id
     * @return 返回执行操作情况
     */
    @GetMapping("/deletesWorkOrder")
    public R deletesWorkOrder(String ids) {
        System.out.println(ids);
        int result = repairService.deletesWorkOrder(ids);
        if (result>0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 工单完成比饼图
     * @return 饼图图需要的数据
     */
    @RequestMapping("/workOrderPie")
    public  R workOrderPie() {
        List<WorkOrderPieVo> list = repairService.workOrderPieData();
        return R.ok().data("workOrderPie", list);
    }

    /**
     *报修类型柱图
     */
    @RequestMapping("/workOrderBar")
    public R workOrderBar() {
        List<TypeWorkBarVo> list = repairService.workOrderBarData();
        return R.ok().data("workOrderBar", list);
    }

    @RequestMapping("/appointList")
    public LayuiPageVo<RepairPersonnel> appointList(@RequestParam Map<String, Object> params) {
        return repairService.appointPageDate(params);
    }

    /**
     *订单指派，为工单表的 personnel_id 赋值选择的职工的id
     */
    @RequestMapping("/giveWork")
    public R giveWork(RepairWorkOrder workOrder) {
        System.out.println(workOrder);
        int result = repairService.giveWork(workOrder);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}
